package com.allwees.bs.c.module.address.service.impl;

import com.allwees.bs.c.module.address.data.AddressDataProvider;
import com.allwees.bs.c.module.address.def.CountryType;
import com.allwees.bs.c.module.address.def.LangType;
import com.allwees.bs.c.module.address.entity.RegionChild;
import com.allwees.bs.c.module.address.entity.RegionCountry;
import com.allwees.bs.c.module.address.entity.RegionLang;
import com.allwees.bs.c.module.address.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址服务实现类
 */
@Service
public class IAddressServiceImpl implements IAddressService {
    @Autowired
    private AddressDataProvider addressDataProvider;

    @Override
    public List<LangType> getLangTypes(CountryType countryType) {
        RegionCountry regionCountry = getRegionCountry(countryType);
        if (regionCountry == null) {
            return null;
        }

        List<LangType> langTypes = new ArrayList<>();
        for (RegionLang regionLang : regionCountry.getLang()) {
            langTypes.add(regionLang.getLangType());
        }

        return langTypes;
    }

    @Override
    public RegionCountry getRegionCountry(CountryType countryType) {
        for (RegionCountry regionCountry : addressDataProvider.getRegionCountries()) {
            if (regionCountry.getCountryType() == countryType) {
                return regionCountry;
            }
        }

        return null;
    }

    @Override
    public List<RegionCountry> getRegionCountries() {
        return addressDataProvider.getRegionCountries();
    }

    @Override
    public RegionLang getRegionLang(CountryType countryType, LangType langType) {
        for (RegionCountry regionCountry : addressDataProvider.getRegionCountries()) {
            if (regionCountry.getCountryType() != countryType) {
                continue;
            }

            for (RegionLang regionLang : regionCountry.getLang()) {
                if (regionLang.getLangType() != langType) {
                    continue;
                }

                return regionLang;
            }
        }

        return null;
    }

    List<RegionChild> getRegionChildWithoutChild(List<RegionChild> regionChildList) {
        List<RegionChild> copyOfRegionChildList = new ArrayList<>();
        for (RegionChild regionChild : regionChildList) {
            RegionChild copyOfRegionChild = new RegionChild();
            copyOfRegionChild.setId(regionChild.getId());
            copyOfRegionChild.setName(regionChild.getName());
            copyOfRegionChild.setRegionType(regionChild.getRegionType());
            copyOfRegionChild.setChild(null);

            copyOfRegionChildList.add(copyOfRegionChild);
        }

        return copyOfRegionChildList;
    }

    private RegionChild getDeepRegionChildById(RegionChild regionChild, String regionChildId) {
        if (regionChild.getId().contentEquals(regionChildId)) {
            return regionChild;
        }

        for (RegionChild one : regionChild.getChild()) {
            if (regionChild.getId().contentEquals(regionChildId)) {
                return one;
            }

            return getDeepRegionChildById(one, regionChildId);
        }

        return null;
    }

    @Override
    public List<RegionChild> getRegionChildById(CountryType countryType, LangType langType, String regionChildId) {
        for (RegionCountry regionCountry : addressDataProvider.getRegionCountries()) {
            if (regionCountry.getCountryType() != countryType) {
                continue;
            }

            for (RegionLang regionLang : regionCountry.getLang()) {
                if (regionLang.getLangType() != langType) {
                    continue;
                }

                for (RegionChild regionChild : regionLang.getChild()) {
                    RegionChild one = getDeepRegionChildById(regionChild, regionChildId);
                    if (one != null) {
                        List<RegionChild> regionChildList = one.getChild();
                        return getRegionChildWithoutChild(regionChildList);
                    }
                }
            }
        }

        return null;
    }
}
